package com.dnw.presenter;

import android.util.Log;

import com.dnw.base.ApiConstant;
import com.dnw.base.BasePresenter;
import com.dnw.base.MyApp;
import com.dnw.modle.Funnys;
import com.dnw.modle.FunnysData;
import com.dnw.modle.FunnysRecord;
import com.dnw.modle.News;
import com.dnw.modle.NewsData;
import com.dnw.modle.Program;
import com.dnw.modle.Result;
import com.dnw.response.FunnysResponse;
import com.dnw.response.FunnysResponseById;
import com.dnw.response.NewsResponse;
import com.dnw.util.ListUtils;
import com.dnw.util.PreUtils;
import com.dnw.util.SharedPreferencesMgr;
import com.dnw.view.lFunnysListView;
import com.dnw.view.lNewsListView;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.socks.library.KLog;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import rx.Subscriber;

import static com.dnw.util.FunnysRecordHelper.getLastFunnysRecord;

/**
 * @author ChayChan
 * @description: 新闻列表的presenter
 * @date 2017/6/18  10:04
 */

public class FunnysListPresenter extends BasePresenter<lFunnysListView> {

    private long lastTime;
    private static final String pageSize = "10";
    private Funnys  funnysById = null;

    public FunnysListPresenter(lFunnysListView view) {
        super(view);
    }


    public void getFunnysList(String channelCode, String pageOn, String searchKey, String labelName) {

        String mChannelCode = null;
        if (!channelCode.equalsIgnoreCase("recommend") && !channelCode.equalsIgnoreCase("searchKey")
                && labelName.equalsIgnoreCase("")) {
            mChannelCode = channelCode;
        }
        try {
            addSubscription(mApiService.getFunnysList(mChannelCode, pageOn + "", pageSize, searchKey.trim(), labelName), new Subscriber<FunnysResponse>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(FunnysResponse response) {
                    lastTime = System.currentTimeMillis() / 1000;
                    PreUtils.putLong(channelCode, lastTime);//保存刷新的时间戳

                    FunnysData data = response.data;
                    List<Funnys> funnysList = data.duanziVOList;
//                List<News> newsList = new ArrayList<>();
//                if (!ListUtils.isEmpty(data)){
//                    for (NewsData newsData : data) {
//                        News news = new Gson().fromJson(newsData.content, News.class);
//                        newsList.add(news);
//                    }
//                }
//                KLog.e(funnysList);
                    mView.onGetFunnysListSuccess(funnysList, response.msg, pageOn);
                }
            });
        } catch (Exception e) {
            Log.e("TAG", "error to get funny list", e);
        }
    }

    public void refreshFunnysList(String channelCode, String pageOn, String searchKey, String  labelName) {
        String x = SharedPreferencesMgr.getString(ApiConstant.SharedPreferencesEnum.PUSH_SHY_ID.name(),"");
        if(!SharedPreferencesMgr.getString(ApiConstant.SharedPreferencesEnum.PUSH_SHY_ID.name(),"").equalsIgnoreCase("")){
            addSubscription(mApiService.getFunnysListById(SharedPreferencesMgr.getString(ApiConstant.SharedPreferencesEnum.PUSH_SHY_ID.name(),"")), new Subscriber<FunnysResponseById>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(FunnysResponseById response) {
                    SharedPreferencesMgr.setString(ApiConstant.SharedPreferencesEnum.PUSH_SHY_ID.name(),"");
                    funnysById = response.data;
                }
            });
        }
        String mChannelCode = null;
        if (!channelCode.equalsIgnoreCase("recommend") && !channelCode.equalsIgnoreCase("searchKey")
                && labelName.equalsIgnoreCase("")) {
            mChannelCode = channelCode;
        }
        try {
            addSubscription(mApiService.getFunnysList(mChannelCode, pageOn + "", pageSize, searchKey.trim(), labelName) , new Subscriber<FunnysResponse>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(FunnysResponse response) {
                    lastTime = System.currentTimeMillis() / 1000;
                    PreUtils.putLong(channelCode, lastTime);//保存刷新的时间戳
                    PreUtils.putInt(channelCode+"_randonLocal", Integer.parseInt(pageOn));

                    FunnysData data = response.data;
                    List<Funnys> funnysList = data.duanziVOList;
                    if(funnysById != null){
                        funnysList.add(0, funnysById);
                    }
//                List<News> newsList = new ArrayList<>();
//                if (!ListUtils.isEmpty(data)){
//                    for (NewsData newsData : data) {
//                        News news = new Gson().fromJson(newsData.content, News.class);
//                        newsList.add(news);
//                    }
//                }
//                KLog.e(funnysList);
                    mView.onRefreshFunnysListSuccess(funnysList, response.msg, pageOn);
                }
            });
        } catch (Exception e) {
            Log.e("TAG", "error to get funny list", e);
        }
    }

    public void progranList() {


        try {
            addSubscription(mApiService.getProgram(), new Subscriber<Result<List<Program>>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Result<List<Program>> result) {

                    mView.onProgramList(result);
                }
            });
        } catch (Exception e) {
            Log.e("TAG", "error to get funny list", e);
        }
    }
}
